package com.smalldoor.thread.demo03;

// 线程测试线程的状态
public class ThreadState {

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println("输出------------------");
        });

        //观察状态
        Thread.State state = thread.getState();
        System.out.println("当前的线程状态为：" + state);

        //重启线程观察状态
        thread.start();
        state = thread.getState();
        System.out.println("重启后的线程状态：" + state);

        while (state != Thread.State.TERMINATED) {
            Thread.sleep(1000);
            state = thread.getState();
            System.out.println("输出还在循环的状态：" + state);
        }

        //停止之后的线程不能再次启动  注释掉会报错
        //thread.start();
    }
}
